//
//  main.m
//  04_HomeWork
//
//  Created by 徐昊 on 16/7/8.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
    NSString *path = @"/Users/xuhao/DLS160710/Objective-C/HomeWork/04_HomeWork/04_HomeWork/crayons.txt";
    // 通过文件路径, 读取文件中的字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@", sourceString);
    
    // 根据\n切个字符串
    NSArray *colorStringArray = [sourceString componentsSeparatedByString:@"\n"];
//    NSLog(@"%@", colorStringArray);
    // 声明存储颜色的可变字典
    NSMutableDictionary *colorDictionary = [NSMutableDictionary dictionary];
    for (int i = 0; i < colorStringArray.count; i++) {
        if ([colorStringArray[i] length] > 0) {
            NSArray *color = [colorStringArray[i] componentsSeparatedByString:@" #"];
            [colorDictionary setObject:[color lastObject] forKey:[color firstObject]];
        }
    }
    NSLog(@"%@", colorDictionary);
    
    // 2.
    NSMutableDictionary *newColorDictionary = [NSMutableDictionary dictionary];
    NSArray *allKeys = colorDictionary.allKeys;
    for (int i = 0; i < allKeys.count; i++) {
        // 取首字母
        NSString *subString = [allKeys[i] substringWithRange:NSMakeRange(0, 1)];
        NSMutableArray *colorArray = [newColorDictionary objectForKey:subString];
        Color *newColor = [[Color alloc] init];
        newColor.name = allKeys[i];
        newColor.colorValue = [colorDictionary objectForKey:allKeys[i]];
        if (colorArray == nil) {
            // 如果数组为空
            colorArray = [NSMutableArray arrayWithObject:newColor];
            [newColorDictionary setObject:colorArray forKey:subString];
        } else {
            // 如果数组不为空
            [colorArray addObject:newColor];
        }
    }
    NSLog(@"%@", newColorDictionary);
    
    
    
    
    
    
    return 0;
}
